package com.lyn.solitude_plugin_host.builder;

import android.app.Application;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.util.Log;

import com.lyn.solitude_plugin_api.entity.Plugin;
import com.lyn.solitude_plugin_api.file.FileUtil;
import com.lyn.solitude_plugin_api.file.Folder;
import com.lyn.solitude_plugin_api.file.FolderFactory;
import com.lyn.solitude_plugin_host.config.PluginFileConfig;

import java.io.File;

/**
 * @author longyn 2024/5/11
 * @version 1.0.0
 */
public class ImpBuilder extends Plugin.Builder {

    private final static String TAG = "ImpBuilder";
    private final ImpBuilderHelper impBuilderHelper;

    public ImpBuilder(Application application, String path) {
        super(application, path);
        impBuilderHelper = new ImpBuilderHelper(application);
    }

    @Override
    public Plugin build() {
        File pluginFile = new File(path);
        if (!pluginFile.exists()) {
            Log.d(TAG, "插件文件不存在");
            return null;
        }
        String md5 = FileUtil.getFileMD5(path);
        if (TextUtils.isEmpty(md5)) {
            return null;
        }
        setId(md5);
        ApplicationInfo pluginApplicationInfo
                = impBuilderHelper.getPluginApplicationInfo(path,
                PackageManager.GET_META_DATA);
        //获取插件名称
        String pluginName = impBuilderHelper.getPluginName(pluginApplicationInfo);
        if (TextUtils.isEmpty(pluginName)) {
            Log.d(TAG, "插件名称未配置");
            return null;
        }
        setName(pluginName);
        Log.d(TAG, "pluginName:" + pluginName);
        String pluginClassName = impBuilderHelper.getPluginClassName(path);
        if (TextUtils.isEmpty(pluginClassName)) {
            Log.d(TAG, "插件第一类名未配置");
            return null;
        }
        Plugin.Mate mate = new Plugin.Mate();
        mate.setId(md5);
        setMate(mate);
        mate.setActivityClassName(pluginClassName);
        //获取插件版本号
        int pluginVersion = impBuilderHelper.getPluginVersion(path);
        if (pluginVersion == -1) {
            Log.d(TAG, "插件版本号未配置");
            return null;
        }
        setVersion(pluginVersion);
        pluginApplicationInfo
                = impBuilderHelper.getPluginApplicationInfo(path,
                PackageManager.GET_ACTIVITIES);
        //获取插件资源res
        Resources pluginResources
                = impBuilderHelper.getPluginResources(pluginApplicationInfo);
        if (pluginResources == null) {
            Log.d(TAG, "获取插件 Resources 失败");
            return null;
        }
        mate.setResources(pluginResources);
        pluginApplicationInfo
                = impBuilderHelper.getPluginApplicationInfo(path,
                PackageManager.GET_META_DATA);
        //获取插件logo
        Drawable pluginLogo = impBuilderHelper.getPluginLogo(pluginApplicationInfo);
        if (pluginLogo == null) {
            Log.d(TAG, "获取插件logo失败");
            return null;
        }
        setLogoDrawable(pluginLogo);
        Folder soFolder = FolderFactory.getFolder(PluginFileConfig.SO);
        //提取插件so
        String soPath = impBuilderHelper.extractPluginSo(path,
                soFolder.getPath(), md5);
        setSoPath(soPath);
        Folder pluginOptimizedFolder = FolderFactory.getFolder(PluginFileConfig.OPTIMIZED);
        ClassLoader pluginClassLoader
                = impBuilderHelper.getPluginClassLoader(path,
                pluginOptimizedFolder.getPath(), soPath);
        if (pluginClassLoader == null) {
            Log.d(TAG, "初始化ClassLoader失败");
            return null;
        }
        mate.setClassLoader(pluginClassLoader);
        return super.build();
    }
}
